// 设置Storage
export function setStorage(key, value) {
    if ((typeof value).toLowerCase() == 'object') {
        value = JSON.stringify(value)
    }
    window.localStorage.setItem(key, value)
}
// 获取Storage
export function getStorage(key) {
    let value = window.localStorage.getItem(key)
    if (value && value.indexOf('{') >= 0 && value.indexOf('}') >= 0) {
        return JSON.parse(value)
    }
    return value
}
// 删除Storage
export function removeStorage(key) {
    window.localStorage.removeItem(key)
}
// 清空Storage
export function clearStorage() {
    window.localStorage.clear()
}

export function dateStr2Stamp(str) {
    let dateStr = str.substring(0, 19)
    dateStr = dateStr.replaceAll('-', '/')
    return new Date(dateStr).getTime()
}
export function formatNum(num) {
    return ('0' + num).slice(-2)
}

export function formatStamp(stamp, fmt) {
    const date = new Date(stamp)
    const opt = {
        'y+': date.getFullYear().toString(), // 年
        'M+': (date.getMonth() + 1).toString(), // 月
        'd+': date.getDate().toString(), // 日
        'h+': date.getHours().toString(), // 时
        'm+': date.getMinutes().toString(), // 分
        's+': date.getSeconds().toString(), // 秒
    }
    for (const k in opt) {
        const ret = new RegExp('(' + k + ')').exec(fmt)
        if (ret) {
            fmt = fmt.replace(
                ret[1],
                ret[1].length === 1
                    ? opt[k]
                    : opt[k].padStart(ret[1].length, '0')
            )
        }
    }
    return fmt
}
export function getRencentDate(days) {
    const today = new Date()
    const stamp = today.getTime() + 3600 * 1000 * 24 * days
    return formatStamp(stamp, 'yyyy-MM-dd hh:mm:ss')
}

import { ElMessage, ElMessageBox } from 'element-plus'
export function showConfirm({
    title = '提示',
    content,
    confirmButtonText = '删除',
    confirmButtonClass = 'button-danger',
    onConfirm,
    onCancel,
}) {
    ElMessageBox.confirm(content, title, {
        confirmButtonText,
        confirmButtonClass,
        draggable: true,
        closeOnClickModal: false,
    })
        .then(() => {
            onConfirm && onConfirm()
        })
        .catch(() => {
            // 取消
            onCancel && onCancel()
        })
}
export function showMsg(message, type = 'success') {
    ElMessage({
        message: message,
        type,
    })
}
// 定义方法，传入参数为图片链接
export function checkImgExists(imgUrl) {
    return new Promise(function (resolve, reject) {
        var ImgObj = new Image()
        ImgObj.src = imgUrl
        ImgObj.onload = function (res) {
            resolve(res)
        }
        ImgObj.onerror = function (err) {
            reject(err)
        }
    })
}
// 判断对象是否为空
export const isEmptyObject = (obj) => {
    return Object.keys(obj).length > 0 ? false : true
}
